package com.app.diary.data.impl;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import androidx.annotation.NonNull;

import com.app.diary.db.DbHelper;
import com.app.diary.data.UserDataSource;

/**
 * 用户数据源的具体实现
 */
public class UserDataSourceImpl implements UserDataSource {

    private DbHelper dbHelper;

    public UserDataSourceImpl(DbHelper dbHelper) {
        this.dbHelper = dbHelper;
    }

    @Override
    public boolean registerUser(String username, String email, String password) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username", username);
        values.put("email", email);
        values.put("password", password); // 注意：在实际应用中应该对密码进行加密存储

        // 检查用户名是否已存在
        if (userExists(username)) {
            return false;
        }

        long newRowId = db.insert(DbHelper.TABLE_USERS, null, values);
        return newRowId != -1;
    }

    @Override
    public boolean validateCredentials(String username, String password) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String[] projection = {"username", "password"};
        String selection = "username = ? AND password = ?";
        String[] selectionArgs = {username, password};

        Cursor cursor = db.query(
                DbHelper.TABLE_USERS,  // 表名
                projection, // 列
                selection,  // 选择条件
                selectionArgs, // 选择参数
                null, // 分组
                null, // 过滤分组后的每一组条目
                null  // 排序
        );

        boolean isValid = cursor.getCount() > 0;
        cursor.close();
        return isValid;
    }

    private boolean userExists(String username) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String[] projection = {"username"};
        String selection = "username = ?";
        String[] selectionArgs = {username};

        Cursor cursor = db.query(
                DbHelper.TABLE_USERS,  // 表名
                projection, // 列
                selection,  // 选择条件
                selectionArgs, // 选择参数
                null, // 分组
                null, // 过滤分组后的每一组条目
                null  // 排序
        );

        boolean exists = cursor.getCount() > 0;
        cursor.close();
        return exists;
    }

    @Override
    public long getUserIdByUsernameAndPassword(String username, String password) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String[] projection = {"id"};
        String selection = "username = ? AND password = ?";
        String[] selectionArgs = {username, password};

        Cursor cursor = db.query(
                DbHelper.TABLE_USERS,  // 表名
                projection, // 列
                selection,  // 选择条件
                selectionArgs, // 选择参数
                null, // 分组
                null, // 过滤分组后的每一组条目
                null  // 排序
        );

        long userId = -1; // 默认无效值
        if (cursor.moveToFirst()) {
            userId = cursor.getLong(cursor.getColumnIndexOrThrow("id"));
        }
        cursor.close();
        return userId;
    }
}